<?php
/**
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  <http://www.gnu.org/licenses/>
 *
 */

/**
 * Framework Sydonie
 *
 * LICENSE : GPLv3
 *
 * @category   Sydonie
 * @package    Sydonie_Form
 * @copyright  Copyright (c) C&F éditions (<http://cfeditions.com>)
 * @copyright  Copyright (c) Université de Caen, 
 *                     laboratoire GREYC (<http://grey.unicaen.fr>)
 * @copyright  Copyright (c) Jean-Marc Lecarpentier
 * @copyright  Copyright (c) Hervé Le Crosnier
 * @licence    http://www.gnu.org/licenses/gpl.html
 *
 * @date de creation : 4 janvier 2005 (2005-01-04)
 * @version  : 4 janvier 2005, 15h12
 *
 */

/**
 * @file FileManager.php Contient la classe utilistaire 
 * Sydonie_FileManager pour  traiter les enregistrements
 *  et effacements de fichiers et répertoires
 *
 * @author Hervé Le Crosnier, Jean-Marc Lecarpentier
 * @important : cette classe s'appuie sur le travail de
 *           Aidan Lister <aidan@php.net>
 * @since Octobre 2007
 */


class Outils_FileManager {


/**
* Delete a file, or a folder and its contents
* Effacement récursif d'un répertoire
*        ou effacement d'un fichier
*
* @author      Aidan Lister <aidan@php.net>
* @version     1.0
* @param       string   $dirname    The directory to delete
* @return      bool     Returns true on success, false on failure
*/
static public function rmdirr($dirname) {
    // Simple delete for a file
    if (is_file($dirname)) {
        return unlink($dirname);
    }

    // Loop through the folder
    $dir = dir($dirname);
    while (false !== $entry = $dir->read()) {
        // Skip pointers
        if ($entry == '.' || $entry == '..') {
            continue;
        }

        // Deep delete directories      
        if (is_dir("$dirname/$entry")) {
            Sydonie_FileManager::rmdirr("$dirname/$entry");
        } else {
            unlink("$dirname/$entry");
        }
    }

    // Clean up
    $dir->close();
    return rmdir($dirname);
}


/**
* Copy a file, or a folder and its contents
* Copie récursive d'un répertoire
*      ou copie d'un fichier
* 
* @author      Aidan Lister <aidan@php.net>
* @version     1.0
* @param       string   $source    The source
* @param       string   $dest      The destination
* @return      bool     Returns true on success, false on failure
*/
  static public function copyr($source, $dest) {
    // Simple copy for a file
    if (is_file($source)) {
        return copy($source, $dest);
    }

    // Make destination directory
    if (!is_dir($dest)) {
        mkdir($dest);
    }

    // Loop through the folder
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
        // Skip pointers
        if ($entry == '.' || $entry == '..') {
            continue;
        }

        // Deep copy directories
        if (is_dir("$source/$entry") && ($dest !== "$source/$entry")) {
            Sydonie_FileManager::copyr("$source/$entry", "$dest/$entry");
        } else {
            copy("$source/$entry", "$dest/$entry");
        }
    }

    // Clean up
    $dir->close();
    return true;
} // fin de la fonction copyr (copie récursive)




} // fin de la classe Sydonie_FileManager
